//
//  NEDConsole.m
//  NEDBugConsole_Example
//
//  Created by 98du on 2021/12/14.
//  Copyright © 2021 林宁宁. All rights reserved.
//

#import "NEDConsole.h"
#import "NEDConsoleWindow.h"
#import "NEDDBConsoleSetting.h"

#import "NEDConsoleCrashHelper.h"
#import "NEDConsoleLogHelper.h"
//#import "NEDConsoleHttpHelper.h"

#import "NEDConsoleMenuViewController.h"
#import "NSObject+NEDKVO.h"

#import <DoraemonKit/DoraemonNetFlowManager.h>


@interface NEDConsole()

@property (retain, nonatomic, readwrite) NSMutableDictionary * envHandleMap;

@property (copy, nonatomic, readwrite) void(^routerHandle)(NSString * routerURL);

@property (retain, nonatomic, readwrite) UINavigationController * consoleNavc;

@property (retain, nonatomic) NSMutableArray <NEDConsoleMenuItem *>* testHandleList;

@end

@implementation NEDConsole


+ (instancetype)console {
    static NEDConsole *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[super allocWithZone:NULL] init];
        instance.envHandleMap = [NSMutableDictionary new];
        instance.testHandleList = [NSMutableArray new];
        [instance ned_kvoForKeyPath:@"rootViewController" observe:[UIApplication sharedApplication].keyWindow block:^(id  _Nonnull obj, id  _Nullable oldVal, id  _Nullable newVal) {
            [NEDConsole hiddenConsoleMenu];
        }];
    });
    return instance;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    return [NEDConsole console];
}

+ (void)enableDebugMode{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{        
        [[NEDConsoleCrashHelper helper] start];
        [[NEDConsoleLogHelper helper] start];
//        [[NEDConsoleHttpHelper helper] start];
        
//        [[DoraemonNetFlowManager shareInstance] canInterceptNetFlow:YES];
    });
}

/// 显示屏幕上的浮动按钮
+ (void)showConsoleFloatWindowMenu{
    [NEDConsoleWindow new];
}

+ (void)hiddenConsoleMenu{
    [[NEDConsole console].consoleNavc dismissViewControllerAnimated:YES completion:nil];
    [NEDConsole console].consoleNavc = nil;
}

+ (void)showConsoleMenu{
    UINavigationController * consoleVC = [NEDConsole console].consoleNavc;
    if(consoleVC.presentingViewController){
        return;
    }
    consoleVC = [[UINavigationController alloc] initWithRootViewController:[[NEDConsoleMenuViewController alloc] init]];
    consoleVC.modalPresentationStyle = UIModalPresentationFullScreen;
    UIViewController* vc = [[[UIApplication sharedApplication].delegate window] rootViewController];
    [vc.presentedViewController?:vc presentViewController:consoleVC animated:YES completion:nil];
    
    [NEDConsole console].consoleNavc = consoleVC;
}


#pragma mark - 环境切换

/// 添加各环境设置
/// @param handle 环境设置回调，返回当前域名信息简略，方便查看
+ (void)addEnv:(NSInteger)env
        handle:(NEDConsoleEnvironmentHandle)handle
   description:(NSString * _Nullable)description{
    [NEDConsole console].envHandleMap[@(env)] = @{@"handle":handle,@"des":description?:@""};
}

+ (NSString *)queryEnvDes:(NSInteger)type{
    return [NEDConsole console].envHandleMap[@(type)][@"des"];;
}
+ (NSString *)queryCurrentEnvDes{
    return [NEDConsole console].envHandleMap[@([NEDDBConsoleSetting sharedInstance].env)][@"des"];;
}

/// 需要环境设置好之后调用
+ (void)updateCurrentEnvironment{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kNEDConsoleEnvironmentChangeNotifation" object:nil];
    NEDConsoleEnvironmentHandle handle = [NEDConsole console].envHandleMap[@([NEDDBConsoleSetting sharedInstance].env)][@"handle"];
    if(!handle){
        return;
    }
    handle();
}

+(NSDictionary *)queryAllEnvMapConfig{
    return [NEDConsole console].envHandleMap;
}

#pragma mark - 路由 url 跳转

+ (void)addRouterHandle:(void(^)(NSString * routerURl))handle{
    [NEDConsole console].routerHandle = handle;
}

#pragma mark - 调试测试

+ (void)addTestDes:(NSString *)testDes handle:(void(^)(void))handle{
    NEDConsoleMenuItem * item = [NEDConsoleMenuItem new];
    item.title = testDes;
    item.handleBlock = handle;
    [[NEDConsole console].testHandleList addObject:item];
}

+ (NSArray <NEDConsoleMenuItem *>*)testAllItems{
    return [NEDConsole console].testHandleList;
}

@end
